﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;

namespace ArgusLib
{
	[AttributeUsage(AttributeTargets.Enum)]
	public class LocalizableEnumAttribute : Attribute
	{
		ResourceManager resourceManager;

		public string ResourceBasename { get; private set; }

		public LocalizableEnumAttribute(string resourceBasename, string assemblyName, string resourceManagerBasename, Type resourceSet)
		{
			this.ResourceBasename = resourceBasename;
			if (string.IsNullOrEmpty(assemblyName) == false)
			{
				Assembly assembly = Assembly.Load(new AssemblyName(assemblyName));
				if (assembly != null && string.IsNullOrEmpty(resourceManagerBasename) == false)
					this.resourceManager = new ResourceManager(resourceManagerBasename, assembly, resourceSet);
			}
		}

		public string GetLocalization(object e)
		{
			if (e == null)
				return null;
			if (this.resourceManager == null)
				return null;
			Type enumType = e.GetType();
			if (enumType.IsEnum == false)
				return null;

			string enumName = Enum.GetName(enumType, e);
			if (string.IsNullOrEmpty(enumName) == true)
				return null;
			return this.resourceManager.GetString(this.ResourceBasename + enumName);
		}
	}

	/// <summary>
	/// Use this <see cref="Attribute"/> to tell that the object the attribute is applied to needs
	/// a specific assembly to be loaded.
	/// </summary>
    public class RequiresAssemblyAttribute : Attribute
    {
		/// <summary>
		/// Full name of the required assembly.
		/// </summary>
		public string FullName { get; set; }

		/// <summary>
		/// Specifies that the object to which the attribute is applied
		/// requires the assembly specified by <paramref name="FullName"/>
		/// to be loaded.
		/// </summary>
		/// <param name="FullName">
		/// The full name of the assembly that will be required.
		/// </param>
		public RequiresAssemblyAttribute(string FullName)
		{
			this.FullName = FullName;
		}

		/// <summary>
		///	Specifies that the object to which the attribute is applied
		///	requires the assembly in which the specified class is defined to be loaded.
		/// </summary>
		/// <param name="type">
		/// A <see cref="Type"/> object representing the class in the assembly
		/// that will be required.
		/// </param>
		public RequiresAssemblyAttribute(Type type)
		{
			Assembly a = Assembly.GetAssembly(type);
			this.FullName = a.FullName;
		}
    }
}
